package com.wzh.store.controller;

import com.wzh.store.service.ICartService;
import com.wzh.store.utils.JsonResult;
import com.wzh.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author wzh
 * @create 2022/4/30 16:05
 * @Description：
 */
@RestController
@RequestMapping("carts")
public class CartController extends BaseController {
    @Autowired
    private ICartService iCartService;
    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCarts(HttpSession session,Integer pid,Integer amount){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        iCartService.addToCart(uid,pid,amount,username);
        return new JsonResult<>(ok,"加入购物车成功");
    }


    @RequestMapping({"","/"})
    public JsonResult<List<CartVo>> getToCart(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<CartVo> lists = iCartService.getCartByUid(uid);
        JsonResult<List<CartVo>> result = new JsonResult<>(ok, lists);
        return result;

    }

    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username= getUsernameFromSession(session);
        Integer data = iCartService.addNum(uid, cid, username);
        JsonResult<Integer> re = new JsonResult<>(ok,data);
        return re;
    }

     @RequestMapping("list")
    public  JsonResult<List<CartVo>> getVoByCid(Integer[] cids,HttpSession session){
//         String username = getUsernameFromSession(session);
         Integer uid = getUidFromSession(session);
         List<CartVo> data = iCartService.getVoByCid(cids, uid);
         JsonResult<List<CartVo>> listJsonResult = new JsonResult<>(ok, data);
         return listJsonResult;
     }

}
